package com.platform.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.entity.PageResult;
import com.platform.common.enums.RoleEnum;
import com.platform.common.result.ApiException;
import com.platform.common.result.ApiExceptionEnum;
import com.platform.user.api.UserApi;
import com.platform.user.dto.UserDTO;
import com.platform.user.model.UserDO;
import com.platform.user.service.UserService;
import com.platform.user.vo.UserVO;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shuc
 */
@RestController
public class UserInternalController implements UserApi {

    @Resource
    private UserService userService;

    @Override
    public String accountToId(String username) {
        return null;
    }

    @Override
    public String idToAccount(String id) {
        return userService.idToAccount(id);
    }

    @Override
    public String idToNickname(String id) {
        return userService.idToNickname(id);
    }


    @Override
    public List<String> queryRolesById(String id) {
        return null;
    }

    @Override
    public Boolean ban(String id) {
        UserDO userDO = UserDO.builder()
                .id(id)
                .role(RoleEnum.BANNED.getName())
                .build();
        Boolean result = userService.updateById(userDO);
        if (!result) {
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        return true;
    }

    @Override
    public PageResult<UserVO> pageUsers(Integer page, Integer size) {
        Page<UserDO> userDOPage = userService.page(page, size);
        return PageResult.<UserVO>builder()
                .total(userDOPage.getTotal())
                .rows(UserDO.toUserVOList(userService.page(page, size)))
                .build();
    }

    @Override
    public Boolean updateById(UserDTO userDTO) {
        Boolean result = userService.updateById(UserDO.from(userDTO));
        if (!result) {
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        return true;
    }

    @Override
    public Boolean removeById(String id) {
        Boolean result = userService.removeById(id);
        if (!result) {
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        return true;
    }
}
